#include "IRSMsgProcess.h"

#include <core/Utility.h>
#include <qbkernel/include/qb_base.h>
#include <qbprotocol/include/MessageParserTool.h>
#include <qbprotocol/include/SSIRSModel.h>
#include <qbprotocol/include/rdProto.h>

using namespace qb;

namespace {
#define Reg_Func(ID) \
  qbBase::getMessageBond()->RegisterDecodeFunc(ID, qbmsg::irs::ExtracFunc)
#define DelPtr(FUNC, TYPE)               \
  case FUNC: {                           \
    TYPE* p = static_cast<TYPE*>(toDel); \
    delete p;                            \
    return nullptr;                      \
  } break;

void* delAck(int func, void* toDel) {
  switch (func) {
    DelPtr(E_FID_IRS_MARKET_DEAL_REQ_V3, xIrsMiddlePriceList_c);
    DelPtr(E_FID_IRS_MARKET_DEAL_PUSH_V3, xIrsMiddlePriceList_c);
    default:
      break;
  }
  return toDel;
}
}  // namespace

void qbmsg::irs::InitDecodeFuncs() {
  Reg_Func(E_FID_IRS_MARKET_DEAL_REQ_V3);
  Reg_Func(E_FID_IRS_MARKET_DEAL_PUSH_V3);
}

void qbmsg::irs::EncodeFunc(qb::proto::Message& msg, qb::SSRequestMsg* req) {
  switch (req->m_FuncID) {
    case E_FID_IRS_MARKET_DEAL_REQ_V3: {
      xIrsMiddlePriceReq_c* pReq = (xIrsMiddlePriceReq_c*)req->m_pRequest;

      qb::proto::Message::List sublist;
      for (auto itr = pReq->m_List.begin(); itr != pReq->m_List.end(); ++itr) {
        sublist.push_back(*itr);
      }
      msg.SetList("xIrsMarketDealReq", sublist);
      break;
    }
    case E_FID_IRS_MARKET_DEAL_PUSH_V3:  // 52046,
                                         // IRS成交摘要信息推送，无请求包体
    default:
      break;
  }
}

void* qbmsg::irs::ExtracFunc(int func, const ProtoMessage& msg, void* toDel) {
  if (toDel != nullptr) return delAck(func, toDel);

  switch (func) {
    case E_FID_IRS_MARKET_DEAL_REQ_V3:
    case E_FID_IRS_MARKET_DEAL_PUSH_V3: {
      ProtoList msglist;
      if (!msg.getList("IMQ_OUTCOME", msglist)) return nullptr;

      xIrsMiddlePriceList_c* ptr = new xIrsMiddlePriceList_c;
      if (!ptr) return nullptr;

      for (const auto& it : msglist) {
        if (it.Type != ProtoValue::VAR_MAP) continue;
        xIrsMiddlePriceUnit_c irsDealUnit;

        const ProtoMap& msgmap = it;
        FIELDCOPY(irsDealUnit.m_id, CParserTool::GetString(msgmap, "id"));
        FIELDCOPY(irsDealUnit.m_company_id,
                  CParserTool::GetString(msgmap, "companyId"));

        FIELDCOPY(irsDealUnit.m_goodsCode,
                  CParserTool::GetString(msgmap, "goodsCode"));
        FIELDCOPY(irsDealUnit.m_typeName,
                  CParserTool::GetString(msgmap, "typeName"));
        FIELDCOPY(irsDealUnit.m_termName,
                  CParserTool::GetString(msgmap, "termName"));
        FIELDCOPY(irsDealUnit.m_method,
                  CParserTool::GetString(msgmap, "method"));

        FIELDCOPY(irsDealUnit.m_price, CParserTool::GetString(msgmap, "price"));
        FIELDCOPY(irsDealUnit.m_preClose,
                  CParserTool::GetString(msgmap, "preClose"));

        CParserTool::MessageGetTime(msgmap, "createTime",
                                    irsDealUnit.m_create_time);

        ptr->m_List.push_back(irsDealUnit);
      }
      return ptr;
      break;
    }
    default:
      break;
  }

  return nullptr;
}
